<template>
  <div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="真实姓名" prop="rel_name">
        <el-input type="text" v-model="ruleForm.rel_name"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    data() {
      let checkName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error("真实姓名不能为空"))
        } else {
          callback();
        }
      };
      let checkUsername = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('用户名不能为空'));
        } else {
          callback();
        }
      };
      let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          rel_name: '',
          username: '',
          pass: '',
          checkPass: ''
        },
        rules: {
          rel_name: [
            {validator: checkName, trigger: 'blur'}
          ],
          username: [
            {validator: checkUsername, trigger: 'blur'}
          ],
          pass: [
            {validator: validatePass, trigger: 'blur'}
          ],
          checkPass: [
            {validator: validatePass2, trigger: 'blur'}
          ],
        }
      };
    },
    methods: {
      async submitForm(formName) {
        const {data: key} = await this.$http.get("getKey");
        let encrypt = new JSEncrypt();
        encrypt.setPublicKey(key);
        // encrypted为加密后的密码
        let encrypted = encrypt.encrypt(this.ruleForm.checkPass);
        this.axios({
          method: 'post',
          url: 'register',
          data: ({
            rel_name: this.ruleForm.rel_name,
            username: this.ruleForm.username,
            password: encrypted
          }),
        }).then((res) => {
          console.log(res);
          if (res.data.code == 200){
            this.$message.success("操作成功！");
            this.$router.push({name: 'Login'})
          }if (res.data.code == 201){
            this.$message.error("用户名已存在，请重新输入。");
            return false;
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped>

</style>
